<?php
// +---------------------------------------------------------------------------+
// | This file is part of the KnowledgebasePublisher package                   |
// | KnowledgebasePublisher - web based knowledgebase publisher tool           |
// |                                                                           |
// | Author:  Evgeny Leontev <eleontev@gmail.com>                              |
// | Copyright (c) 2005 Evgeny Leontev                                         |
// |                                                                           |
// | For the full copyright and license information, please view the LICENSE   |
// | file that was distributed with this source code.                          |
// +---------------------------------------------------------------------------+

require_once $controller->working_dir . 'inc/User.php';
require_once $controller->working_dir . 'inc/UserModel.php';


// initialize objects
$rq =& new RequestData($_GET);
$rp =& new RequestData($_POST);

$obj = new User;
$obj->is_admin = (isset($_SESSION['priv_']['all']));
$auth = &new AuthPriv();
$manager =& $obj->setManager(new UserModel());
$manager->checkPriv($priv, $controller->action, @$rq->id);


switch ($controller->action) {
case 'delete': // ------------------------------
	
	if(APP_DEMO_MODE) { $controller->go('not_allowed_demo', true); }
	
	$manager->delete($rq->id);
	$controller->go();
	
	break;
	

case 'status': // ------------------------------
	
	if(APP_DEMO_MODE) { $controller->go('not_allowed_demo', true); }
	
	$manager->status($rq->status, $rq->id);
	$controller->go();

	break;
	
	
case 'insert': // ------------------------------
case 'update': // ------------------------------
	
	if(isset($rp->submit)) {
		
		if(APP_DEMO_MODE) { $controller->go('not_allowed_demo', true); }
		
		$is_error =& $obj->validate($rp->vars, $manager);
		
		if($is_error) {
			$rp->stripVars(true);
			
			$obj->set($rp->vars);
			$obj->setPriv(@$rp->vars['priv']);
		
		} else {
			$rp->stripVars();
			
			$obj->setPassword($rp->not_change_pass); // mean not insert in db if not_change_pass = 1
			$obj->set($rp->vars);
			$obj->setPriv(@$rp->vars['priv']);
			
			$manager->save($obj, $auth->getUserId());
			$controller->go();
		}
	
	} elseif($controller->action == 'update') {
		
		$data = &$manager->getById($rq->id);
		$rp->stripVarsValues($data);
		$obj->set($data);
		
		$data = &$manager->getPrivById($rq->id);
		$rp->stripVarsValues($data);
		$obj->setPriv($data);		
	}
	
	$view = &$controller->getView($obj, $manager, 'UserView_form');

	break;


default: // ------------------------------------
	
	$view = &$controller->getView($obj, $manager, 'UserView_list');
}
?>